// 工程化
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 创建路由模块对象，配置路由
const router = new VueRouter({
  // 添加路由配置
  routes: [
    {
      path: '/',
      redirect: { name: 'login' }
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue')
    },
    {
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue'),
      // 添加重定向，一进入到index就渲染hello
      redirect: { name: 'hello' },
      // 通过childrend属性配置嵌套路由
      children: [
        {
          name: 'hello',
          path: 'hello',
          component: () => import('@/views/hello.vue')
        },
        {
          name: 'postlist',
          path: 'postlist',
          component: () => import('@/views/postlist.vue')
        },
        {
          name: 'postPublish',
          path: 'postPublish',
          component: () => import('@/views/postPublish.vue')
        }
      ]
    }
  ]
})

// 添加导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next()
  } else {
    if (localStorage.getItem('heima_back_token_59')) {
      next()
    } else {
      next({ name: 'login' })
    }
  }
})

// 暴露
export default router